package com.sk.weichat.adapter;


import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.MyApplication;
import com.sk.weichat.R;
import com.sk.weichat.bean.User;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.util.DateUtils;
import com.sk.weichat.util.DisplayUtil;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.view.HeadView;

import java.util.Date;
import java.util.List;

public class FriendHeadAdapter extends BaseQuickAdapter<User, BaseViewHolder> {

    public FriendHeadAdapter(int layoutResId, @Nullable List<User> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, User item) {

        helper.addOnClickListener(R.id.layout_call_item);

        helper.setText(R.id.tv_call_nickname, item.getNickName());
        HeadView imgAvatar = helper.getView(R.id.img_call_avatar);
        CheckBox checkBoxSex = helper.getView(R.id.checkbox_call_sex);//性别、年龄
        TextView tvAnonymity = helper.getView(R.id.tv_call_anonymity);//匿名或显示距离、登录时间

        //距离、登录时间
        double latitude = MyApplication.getInstance().getBdLocationHelper().getLatitude();
        double longitude = MyApplication.getInstance().getBdLocationHelper().getLongitude();
        String distance = DisplayUtil.getDistanceInfo(latitude, longitude, item.getLoc());

        if (item.getSettings() == null) {
            tvAnonymity.setVisibility(View.INVISIBLE);
        } else {
            if (item.getSettings().getShowLastLoginTime() == 1 && item.getSettings().getAllowQueryLoc() == 1) {//显示登录时间，距离
                if (TextUtils.isEmpty(distance)) {
                    tvAnonymity.setText(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getShowLastLoginTime()));
                } else if (TextUtils.isEmpty(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getShowLastLoginTime()))){
                    tvAnonymity.setText(distance);
                }else {
                    tvAnonymity.setText(distance + " · " + TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getShowLastLoginTime()));
                }
            } else if (item.getSettings().getShowLastLoginTime() == 1) {
                tvAnonymity.setText(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getShowLastLoginTime()));//只显示登录时间
            } else if (item.getSettings().getAllowQueryLoc() == 1) {//只显示距离
                if (TextUtils.isEmpty(distance)) {
                    tvAnonymity.setVisibility(View.INVISIBLE);
                } else {
                    tvAnonymity.setText(distance);
                }

            } else {
                tvAnonymity.setText("隐身");
            }
        }

        //性别
        if (item.getSex() == 0) {//女
            checkBoxSex.setChecked(false);
        } else {
            checkBoxSex.setChecked(true);
        }
        checkBoxSex.setText(DateUtils.getAge(new Date(item.getBirthday() * 1000)) + "");

        //头像
        AvatarHelper.getInstance().displayAvatar("", item.getUserId(), imgAvatar.getHeadImage(), true);
    }
}